package main;

import graphics.Drawing;

import graphics.OldGameWindow;
import graphics.MaxGraphics;

import io.*;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.imageio.ImageIO;

import ship.Ship;
import structure.Incremental;
import util.Point;
import util.RunThread;
import util.Timer;

public class ThinGame implements Incremental {
	private int player;
	private Vector<String> info = new Vector<String>();

	private OldGameWindow window;

	private BufferedImage static1, static2;
	private Timer staticTimer = new Timer(60, true);
	private BufferedImage buffer = new BufferedImage(OldGameWindow.WINDOW_X, OldGameWindow.WINDOW_Y, BufferedImage.TYPE_INT_BGR);

	private MaxGraphics g = new MaxGraphics((Graphics2D) buffer.getGraphics());

	private Ship ship;

	private ClientShipListener listener;
	private RunThread run;
	
	private Network net;

	public static ThinGame game;

	public ThinGame(int player, Network net) {
		ThinGame.game = this;
		staticTimer.start();
		try {
			static1 = ImageIO.read(new File("images/static1.png"));
			static2 = ImageIO.read(new File("images/static2.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.player = player;
		this.net = net;
		ship = LevelLoader.getShip("data/level.txt", player);
		window = new OldGameWindow();
		window.begin();
		prep();
		listener = new ClientShipListener();
		run = new RunThread(this);
		run.start();
	}

	public void recieveMessage(String s) {
		try {
			if(s.equals("Sync")) {
				sync();
			}
			else {
				draw(s);
			}
		}
		catch(Exception e) {

		}
	}
	
	public void sendMessage(String s) {
		net.sendMessage(s);
	}

	public void draw(String s) {
		int width = window.getWidth();
		int height = window.getHeight();

		StringTokenizer tok = new StringTokenizer(s);
		String type = tok.nextToken();
		

		if(type.equals("Ship")) {
			double player = Double.parseDouble(tok.nextToken());

			double x = Double.parseDouble(tok.nextToken());
			double y = Double.parseDouble(tok.nextToken());
			double angle = Double.parseDouble(tok.nextToken());
			if(player == this.player) {
				ship.adjust(new Point(x, y), angle);
			}
			g.translate((-ship.getX() + (width/2)), (-ship.getY() + (height/2)));
			double shields = Double.parseDouble(tok.nextToken());
			double shieldCycle = Double.parseDouble(tok.nextToken());
			Drawing.drawShip(g, new Point(x, y), angle, shields, shieldCycle);
		}
		if(type.equals("Asteroid")) {
			g.translate((-ship.getX() + (width/2)), (-ship.getY() + (height/2)));
			double x = Double.parseDouble(tok.nextToken());
			double y = Double.parseDouble(tok.nextToken());
			double xRad = Double.parseDouble(tok.nextToken());
			double yRad = Double.parseDouble(tok.nextToken());
			Drawing.drawAsteroid(g, new Point(x, y), new Point(xRad, yRad));
		}
		if(type.equals("Shot")) {
			g.translate((-ship.getX() + (width/2)), (-ship.getY() + (height/2)));
			double x = Double.parseDouble(tok.nextToken());
			double y = Double.parseDouble(tok.nextToken());
			double lastX = Double.parseDouble(tok.nextToken());
			double lastY = Double.parseDouble(tok.nextToken());
			Drawing.drawShot(g, new Point(x, y), new Point(lastX, lastY));
		}
		g.translate(-(-ship.getX() + (width/2)), -(-ship.getY() + (height/2)));
	}

	public void sync() {
		window.getGraphics().drawImage(buffer, 0, 0, null);
		prep();
	}

	public void prep() {

		drawStatic(g);

		int width = window.getWidth();
		int height = window.getHeight();
		g.translate((-ship.getX() + (width/2)), (-ship.getY() + (height/2)));

		g.setColor(Color.black);
		g.fillOval(ship.getX(), ship.getY(), ship.getSensorRadius(), ship.getSensorRadius());
		g.translate(-(-ship.getX() + (width/2)), -(-ship.getY() + (height/2)));
	}

	public void drawStatic(MaxGraphics g) {
		staticTimer.count();
		BufferedImage temp = null;
		if(staticTimer.getValue() > 30) {
			temp = static1;
		}
		else
			temp = static2;
		for(int i = 0; i < 1500; i = i + 500) {
			for(int j = 0; j < 1500; j = j+500) {
				g.drawImage(temp, i, j);
			}
		}
	}
	
	public Ship getShip() {
		return ship;
	}
	
	public OldGameWindow getWindow() {
		return window;
	}

	@Override
	public void increment() {
		listener.update();
	}
}

